//
//  AGoodsViewController.m
//  MagazineShangejie
//
//  Created by cnbizMedia on 16/4/19.
//  Copyright © 2016年 CnbizMedia. All rights reserved.
//、

#import "AGoodsViewController.h"
#import "Util.h"
#import "Masonry.h"
#import "OrderForGoodsViewController.h"

@implementation AGoodsViewController{
    
    PageControlScrollView*Pscroll;//头图
    __weak IBOutlet UITableView *goodsTable;
    NSMutableDictionary *goodsDic;
    
    
    UIView *backView;
    UIView *whiteView;
    UIImageView *goodsIamge;
    
    UILabel *goodsNumberLabel;
    int goodsNumber;
    
    UITextField *MessageField;
}

-(void)viewWillAppear:(BOOL)animated{


}

-(void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:YES];

    
}

-(void)viewDidLoad{

    [super viewDidLoad];
    
    goodsDic = [UserDefaults objectForKey:@"goodsDic"];
//    NSLog(@"get goodsDic %@ ",goodsDic);
    
    goodsNumber = 1;
    
    [self tableHeader];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

-(UIView*)tableHeader{
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 480)];
    UIView*Bview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 430)];
    Bview.backgroundColor = [UIColor whiteColor];
    view.backgroundColor  = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    
    [view addSubview:Bview];
    
    
    
    self.title = goodsDic[@"name"];
    
    
    NSMutableArray *goodsTopArray = [UserDefaults objectForKey:@"goodsTopArray" ];
//    NSLog(@"get goodsTopArray %@ ",goodsTopArray);
    
    
    NSMutableArray*array=[[NSMutableArray alloc]init];
    ///滚动头图
    for (NSDictionary*dic in goodsTopArray) {
        PageImageView*view=[[PageImageView alloc]init];
        view.All_Dic=dic;
        view.ID=dic[@"id"];
        view.PCatid=dic[@"catid"];
        view.imageUrl=dic[@"pic"][0];
        view.PTitle=dic[@"title"];
        view.URL=dic[@"url"];
        //                [array addObject:view];
        //                [array insertObject:view atIndex:0];
        [array addObject:view];
    }
    
    //                [Pscroll refresh:array];
    float h = 290;
    if (self.view.frame.size.width>320) {
        h+=45;
        
        [view setFrame:CGRectMake(0, 0, self.view.frame.size.width, 515)];
        [Bview setFrame:CGRectMake(0, 0, self.view.frame.size.width, 465)];
    }
    
    Pscroll = [[PageControlScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, h) pictures:array whichView:@"mall"];//字符串是用来区分不同页面的滚动头图的区别的，显示效果不同.这里有时间要进行更换
    Pscroll.delegate = self;
    
    [Bview addSubview:Pscroll];
    
    UIImageView *topImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, h)];
    [topImage sd_setImageWithURL:[NSURL URLWithString:goodsDic[@"pic"][0]] placeholderImage:[UIImage imageNamed:@"icon_default_ipad"]];
    [Bview addSubview:topImage];
    
    
    UILabel *titleLabel = [[UILabel alloc]init];
    [titleLabel setFrame:CGRectMake(15, Pscroll.frame.size.height + 12, 300, 30)];
    
    [Bview addSubview:titleLabel];
    titleLabel.text = goodsDic[@"name"];
    titleLabel.font = [UIFont systemFontOfSize:20.f];

    UILabel *newMoneyLabel = [[UILabel alloc]init];
    [newMoneyLabel setFrame:CGRectMake(15, Pscroll.frame.size.height + 45, 110, 30)];
//    newMoneyLabel.backgroundColor = [UIColor greenColor];
    [Bview addSubview:newMoneyLabel];
    newMoneyLabel.text =  [NSString stringWithFormat:@"￥%@",goodsDic[@"discount_price"]] ;
    newMoneyLabel.textColor = [UIColor redColor];
    newMoneyLabel.font = [UIFont systemFontOfSize:20.f];

    UILabel *redLabel = [[UILabel alloc]init];
    [redLabel setFrame: CGRectMake(130, Pscroll.frame.size.height + 51, 80, 20)];
//    [Bview addSubview:redLabel];
    redLabel.backgroundColor = [UIColor redColor];
    redLabel.textColor = [UIColor whiteColor];
    redLabel.font = [UIFont fontWithName:nil size:13];
    redLabel.text = @"新春特惠";
    redLabel.textAlignment = NSTextAlignmentCenter;
    
    
    NSString *oldPrice = [NSString stringWithFormat:@"原价:￥%@",goodsDic[@"price"]] ;
    NSUInteger length = [oldPrice length];
    
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:oldPrice];
    [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(2, length-2)];
    //    [attri addAttribute:NSStrikethroughColorAttributeName value:UIColorFromRGB(0x999999, 1) range:NSMakeRange(2, length-2)];
    [attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor grayColor] range:NSMakeRange(2, length-2)];
    
    UILabel *oldMoneyLabel = [[UILabel alloc] init];
    [oldMoneyLabel setFrame:CGRectMake(15, Pscroll.frame.size.height + 70, 150, 25)];
    [Bview addSubview:oldMoneyLabel];
    oldMoneyLabel.font = [UIFont systemFontOfSize:15];
    oldMoneyLabel.textColor = [UIColor grayColor];
    //    oldMoneyLabel.text = @"$998";
    [oldMoneyLabel setAttributedText:attri]; //label显示的字体上是否需要加删除线
    
    if ([goodsDic[@"discount_price"] isEqualToString:@"0.00"]) {
        newMoneyLabel.text = [NSString stringWithFormat:@"￥%@",goodsDic[@"price"]] ;
        
        oldMoneyLabel.hidden = YES;
    }
    
    UILabel *fastMailLabel = [[UILabel alloc] init];
    [fastMailLabel setFrame:CGRectMake(15, Pscroll.frame.size.height + 95, 100, 25)];
    [Bview addSubview:fastMailLabel];
    fastMailLabel.font = [UIFont systemFontOfSize:15];
    fastMailLabel.textColor = [UIColor lightGrayColor];
    fastMailLabel.text = [NSString stringWithFormat:@"快递:￥%@",goodsDic[@"shipping_base_fee"] ];
    

    UILabel *lostLabel = [[UILabel alloc] init];
    [lostLabel setFrame:CGRectMake((view.frame.size.width - 80) / 2, Pscroll.frame.size.height + 95, 80, 25)];
    [Bview addSubview:lostLabel];
    lostLabel.font = [UIFont systemFontOfSize:15];
    lostLabel.textColor = [UIColor lightGrayColor];
    lostLabel.textAlignment = NSTextAlignmentCenter;
    lostLabel.text = [NSString stringWithFormat:@"剩余:%@",goodsDic[@"num"] ];
    
    UILabel *sealLabel = [[UILabel alloc] init];
    [sealLabel setFrame:CGRectMake((view.frame.size.width - 115)  , Pscroll.frame.size.height + 95, 100, 25)];
//    [Bview addSubview:sealLabel];
    sealLabel.font = [UIFont systemFontOfSize:15];
    sealLabel.textColor = [UIColor lightGrayColor];
    sealLabel.textAlignment = NSTextAlignmentRight;
    sealLabel.text = @"已卖出:10";
    
    
    UILabel *noteLabel = [[UILabel alloc] init];
    [noteLabel setFrame:CGRectMake(15  , Bview.frame.size.height + 10, 300, 30)];
    [view addSubview:noteLabel];
    noteLabel.font = [UIFont systemFontOfSize:20];
    noteLabel.textColor = [UIColor grayColor];
    noteLabel.text = @"本商品由商界传媒电商平台提供";
    
    
    goodsTable.tableHeaderView.frame=CGRectMake(0, 0, self.view.frame.size.width,500 );//view.frame.size.height
    goodsTable.tableHeaderView = view;
    //    [aTable.tableHeaderView addSubview:view];
//    headerView=view;
    
    return view;
}



#pragma mark - Table view data source
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //    if (section==0) {
    ////        return [self tableHeader];
    //        aTable.tableHeaderView = headerView;
    //        return headerView;
    //
    
    
    //    aTable.tableHeaderView = headerView;
    //    if (section > 0) {
    //        UIView *littleHeadView = [[UIView alloc]init];
    //        littleHeadView.backgroundColor = [UIColor lightTextColor];
    //        return littleHeadView;
    //    }
    //    else{
    //        return nil;
    //    }
    
    
    //    UIView *littleHeadView = [[UIView alloc]init];
    //    littleHeadView.backgroundColor = [UIColor lightTextColor];
    //    return littleHeadView;
    
    //    if (section==0) {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 0, tableView.bounds.size.width, tableView.sectionHeaderHeight)];
    
//    view.backgroundColor =  [[UIColor alloc] initWithRed:100/255.f green:240/255.f blue:240/255.f alpha:1];
    view.backgroundColor =  [UIColor whiteColor];
    
    UILabel *noticeLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 180, 30)];
    noticeLabel.text = @"商品描述";
    noticeLabel.font = [UIFont fontWithName:nil size:20.f];
    [view addSubview:noticeLabel];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(10, 49, self.view.frame.size.width, 1)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:lineView];
    
    return view;
    
    //    }
    
    //    else{
    //        UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
    //
    //        UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    //        headerLabel.backgroundColor = [UIColor clearColor];
    //        headerLabel.opaque = NO;
    //        headerLabel.textColor = [UIColor lightGrayColor];
    //        headerLabel.highlightedTextColor = [UIColor whiteColor];
    //        headerLabel.font = [UIFont boldSystemFontOfSize:20];
    //        headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);
    //
    //        if (section == 0) {
    //            headerLabel.text =  @"测试1";
    //        }else if (section == 1){
    //            headerLabel.text = @"测试2";
    //        }else if (section == 2){
    //            headerLabel.text = @"测试3";
    //        }else if (section == 3){
    //            headerLabel.text = @"测试4";
    //        }
    //
    //        [customView addSubview:headerLabel];
    //
    ////        return customView;
    //        return nil;
    //    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section==0) {
        return 1;
    }
    else if (section==1) {
        return 1;
    }
    else{
        return 0;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
            if (indexPath.section==0) {
                
                
                
                
                
                NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[goodsDic[@"content"] dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
                
                
                
                UILabel *goodsNoteLabel = [[UILabel alloc]init];
                goodsNoteLabel.attributedText = attrStr;
                goodsNoteLabel.font = [UIFont systemFontOfSize:15];
                
                
                [goodsNoteLabel setNumberOfLines:0];
                
                goodsNoteLabel.lineBreakMode = NSLineBreakByWordWrapping;
                
                CGSize size = [goodsNoteLabel sizeThatFits:CGSizeMake(self.view.frame.size.width - 30, MAXFLOAT)];
                
//                NSLog(@"size 自动label的大小 %@",size);
                return size.height + 80;
                
            }else{
                return 45;
            }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        
        return 50;
  
    }else{
        return 15;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell ;
    //    if(indexPath.section==0){
    //        cell = [tableView dequeueReusableCellWithIdentifier:@"setcellaa" forIndexPath:indexPath];
    //    }else{
    cell = [tableView dequeueReusableCellWithIdentifier:@"goodsCell" forIndexPath:indexPath];
    //    }
    if (cell==nil) {
        //        if(indexPath.section==0){
        //            cell=[tableView dequeueReusableCellWithIdentifier:@"setcellaa"];
        //        }else{
        //            cell=[tableView dequeueReusableCellWithIdentifier:@"setcell"];
        
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"goodsCell"];
        //        }
    }
    
    
    
    
    
    
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[goodsDic[@"content"] dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    
    
    
    UILabel *goodsNoteLabel = [[UILabel alloc]init];
    goodsNoteLabel.attributedText = attrStr;
    goodsNoteLabel.font = [UIFont systemFontOfSize:15];

    
    [goodsNoteLabel setNumberOfLines:0];
    
    goodsNoteLabel.lineBreakMode = NSLineBreakByWordWrapping;

    CGSize size = [goodsNoteLabel sizeThatFits:CGSizeMake(self.view.frame.size.width - 30, MAXFLOAT)];
    
    goodsNoteLabel.frame =CGRectMake(15, 10, self.view.frame.size.width - 30 , size.height);
    
    [cell.contentView addSubview:goodsNoteLabel];
    
//initWithFrame:CGRectMake(15, 15, self.view.frame.size.width - 30, <#CGFloat height#>)

    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 200, 20)];
    label.font = [UIFont boldSystemFontOfSize:20.0f];  //UILabel的字体大小
    label.numberOfLines = 0;  //必须定义这个属性，否则UILabel不会换行
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentLeft;  //文本对齐方式
    [label setBackgroundColor:[UIColor redColor]];
    
    //宽度不变，根据字的多少计算label的高度
//    NSString *str = @"可以更改此内容进行测试，宽度不变，高度根据内容自动调节";
//    CGSize size = [str sizeWithFont:label.font constrainedToSize:CGSizeMake(label.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
//    //根据计算结果重新设置UILabel的尺寸
//    [label setFrame:CGRectMake(0, 10, 200, size.height)];
    
    
    
    //    UIImageView*imageView=(UIImageView*)[cell.contentView viewWithTag:11];
//    UILabel*label=(UILabel*)[cell.contentView viewWithTag:22];
//    
//    //    if (indexPath.section==0){
//    //        if (indexPath.row==0) {
//    //            imageView.image=[UIImage imageNamed:@"vipbtn"];
//    //            label.text=@"查看VIP特权";
//    //
//    //
//    //        }}
//    //
//    //    else
//    
//    //    cell.backgroundColor = [UIColor lightGrayColor];
//    
//    
//    if (indexPath.section==0){
//        if (indexPath.row==0) {
//            imageView.image=[UIImage imageNamed:@"userMessageIcon"];
//            label.text=@"我的消息";
//            
////            [label setMyAppCustomBadgeValue:app.BadgeStr];
//            
//        }else if (indexPath.row==1){
//            imageView.image=[UIImage imageNamed:@"userPayIcon"];
//            label.text=@"支付订单";
//        }else if (indexPath.row==2){
//            
//            imageView.image=[UIImage imageNamed:@"club"];
//            label.text=@"商界俱乐部";
//            
//        }
//        
//    }else if (indexPath.section==1){
//        imageView.image=[UIImage imageNamed:@"userShareIcon"];
//        label.text=@"把看商界分享给朋友";
//        
//    }else if (indexPath.section==2){
//        imageView.image=[UIImage imageNamed:@"userTuCaoIcon"];
//        label.text=@"吐槽";
//        
//    }
//    else if (indexPath.section==4){
//        imageView.image=[UIImage imageNamed:@"userSetIcon"];
//        label.text=@"设置";
//        
//    }
//    else if (indexPath.section==3){
//        imageView.image=[UIImage imageNamed:@"userSysIcon"];
//        label.text=@"扫一扫";
//        
//    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];//点击后不会有阴影
    
    //    [self.navigationController setNavigationBarHidden:NO animated:true];
    //    if (indexPath.section==0){
    //        self.hidesBottomBarWhenPushed = YES;
    //        //        [self performSegueWithIdentifier:@"pushVIP" sender:self];
    //
    //        UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    //        ChooseVIPViewController *choo=[story instantiateViewControllerWithIdentifier:@"ChooseVIPViewController"];
    //        //        opin.view.backgroundColor=[UIColor colorWithWhite:0.890 alpha:1.000];
    //        //        [opin opinionInWWW:1];
    //        [self.navigationController pushViewController:choo animated:YES];
    //
    //    }
    //    else
    
}


- (IBAction)buyTheGoods:(id)sender {
    
    
    [self makeTheBuyView];
}


-(void)makeTheBuyView{

    backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    backView.backgroundColor = [UIColor colorWithHue:0.1 saturation:0.1 brightness:0.1 alpha:0.6];
    [self.view addSubview:backView];
    
    
    whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 325, self.view.frame.size.width, 325)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView];

    goodsIamge = [[UIImageView alloc]initWithFrame:CGRectMake(15, self.view.frame.size.height - 355, 95, 95)];
    [goodsIamge sd_setImageWithURL:[NSURL URLWithString:goodsDic[@"pic"][0]] placeholderImage:[UIImage imageNamed:@"icon_default_ipad"]];
    [self.view addSubview:goodsIamge];
    
    
    UIButton *goodsxxButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 25, 10, 15, 15)];
    [goodsxxButton setImage: [UIImage imageNamed:@"goodsxx" ]  forState:UIControlStateNormal];
    [goodsxxButton addTarget:nil action:@selector(goodsxxEvent) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:goodsxxButton];
    
    
    
    UILabel *newMoneyLabel = [[UILabel alloc]init];
    [newMoneyLabel setFrame:CGRectMake(125, 8, 110, 20)];
    //    newMoneyLabel.backgroundColor = [UIColor greenColor];
    [whiteView addSubview:newMoneyLabel];
    newMoneyLabel.text =  [NSString stringWithFormat:@"￥%@",goodsDic[@"discount_price"]] ;
    newMoneyLabel.textColor = [UIColor redColor];
    newMoneyLabel.font = [UIFont systemFontOfSize:16.f];

    if ([goodsDic[@"discount_price"] isEqualToString:@"0.00"]) {
        newMoneyLabel.text = [NSString stringWithFormat:@"￥%@",goodsDic[@"price"]] ;
        
    }
    
    
    UILabel *lostLabel = [[UILabel alloc] init];
    [lostLabel setFrame:CGRectMake(125, 30, 150, 20)];
    [whiteView addSubview:lostLabel];
    lostLabel.font = [UIFont systemFontOfSize:15];
    lostLabel.textColor = [UIColor blackColor];
    lostLabel.text = [NSString stringWithFormat:@"剩余:%@",goodsDic[@"num"] ];
    
    UILabel *choseLabel = [[UILabel alloc] init];
    [choseLabel setFrame:CGRectMake(125, 50, 150, 20)];
    [whiteView addSubview:choseLabel];
    choseLabel.font = [UIFont systemFontOfSize:15];
    choseLabel.textColor = [UIColor blackColor];
//    choseLabel.text = @"选择:型号";
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(10, 80, self.view.frame.size.width - 20, 1)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [whiteView addSubview:lineView];
    
    
    UILabel *typeLabel = [[UILabel alloc] init];
    [typeLabel setFrame:CGRectMake(15, 90, 150, 20)];
    [whiteView addSubview:typeLabel];
    typeLabel.font = [UIFont systemFontOfSize:15];
    typeLabel.textColor = [UIColor blackColor];
    typeLabel.text = @"购买数量";
    
    
    
    
    
    
    //型号和购买数量的分割线
    UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(10, 120, self.view.frame.size.width - 20, 1)];
    lineView2.backgroundColor = [UIColor lightGrayColor];
    [whiteView addSubview:lineView2];
    
    
    UILabel *numberLabel = [[UILabel alloc] init];
    [numberLabel setFrame:CGRectMake(15, 135, 150, 20)];
    [whiteView addSubview:numberLabel];
    numberLabel.font = [UIFont systemFontOfSize:15];
    numberLabel.textColor = [UIColor blackColor];
    numberLabel.text = @"买家留言:";
    
    
    MessageField = [[UITextField alloc] init];
    [MessageField setFrame:CGRectMake(90, 135, 220, 20)];
    MessageField.placeholder = @"请输入产品的颜色尺码";
//    MessageField.backgroundColor = [UIColor redColor];
    [whiteView addSubview:MessageField];
    
    UIView *lineView3 = [[UIView alloc]initWithFrame:CGRectMake(10, 165, self.view.frame.size.width - 20, 1)];
    lineView3.backgroundColor = [UIColor lightGrayColor];
    [whiteView addSubview:lineView3];

    
    
    UIButton *jianButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 85, 88, 23, 23)];
    [jianButton setImage: [UIImage imageNamed:@"goodsjian" ]  forState:UIControlStateNormal];
    [jianButton addTarget:nil action:@selector(jianEvent) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:jianButton];
    
    UIButton *jiaButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 37, 88, 23, 23)];
    [jiaButton setImage: [UIImage imageNamed:@"goodsjia" ]  forState:UIControlStateNormal];
    [jiaButton addTarget:nil action:@selector(jiaEvent) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:jiaButton];
    
    UIImageView *numberImg = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 61, 88, 23, 23)];
    numberImg.image = [UIImage imageNamed:@"goodsNumber"];
    [whiteView addSubview:numberImg];
    
    goodsNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 63, 88, 26, 23)];
    goodsNumberLabel.textAlignment = NSTextAlignmentCenter;
    goodsNumberLabel.text = [NSString stringWithFormat:@"%d",goodsNumber];
    [whiteView addSubview:goodsNumberLabel];
    
    UIButton *buyButton = [[UIButton alloc]initWithFrame:CGRectMake(0, whiteView.frame.size.height - 49, self.view.frame.size.width, 49)];
    buyButton.backgroundColor = [UIColor redColor];
    [buyButton setTitle:@"确定" forState:UIControlStateNormal];
    buyButton.titleLabel.textColor = [UIColor whiteColor];
    [buyButton addTarget:nil action:@selector(gotoBuy) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:buyButton];
}

-(void)gotoBuy{
    
    [UserDefaults setObject:[NSString stringWithFormat:@"%d",goodsNumber] forKey:@"goodsNumber"];
    [UserDefaults setObject:MessageField.text forKey:@"goodsMessage" ];
    
    if (![UserDefaults objectForKey:@"user"]) {
        UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        LoginViewController*login=[story instantiateViewControllerWithIdentifier:@"LoginViewController"];
        [self.navigationController pushViewController:login animated:YES];
        
    }else{
    
    self.hidesBottomBarWhenPushed = YES;
    UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    OrderForGoodsViewController*login=[story instantiateViewControllerWithIdentifier:@"OrderForGoodsViewController"];
    UINavigationController*nav=[[UINavigationController alloc]initWithRootViewController:login];
    [self.navigationController pushViewController:login animated:YES];
//    self.hidesBottomBarWhenPushed = NO;
    }
}

- (void)goodsxxEvent{

     backView.hidden = YES;
     whiteView.hidden = YES;
     goodsIamge.hidden = YES;
}

- (void)jianEvent{
    if (goodsNumber > 1) {
        goodsNumberLabel.text = [NSString stringWithFormat:@"%d", --goodsNumber];
        
    }

}


- (void)jiaEvent{

    if (goodsNumber == [goodsDic[@"num"] integerValue]) {
        return;
    }
    goodsNumberLabel.text = [NSString stringWithFormat:@"%d", ++goodsNumber];
    
}


#pragma mark - keyboard events -

///键盘显示事件
- (void) keyboardWillShow:(NSNotification *)notification {
    //获取键盘高度，在不同设备上，以及中英文下是不同的
    CGFloat kbHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    
    //计算出键盘顶端到inputTextView panel底端的距离(加上自定义的缓冲距离INTERVAL_KEYBOARD)
//    CGFloat offset = (panelInputTextView.frame.origin.y+panelInputTextView.frame.size.height+INTERVAL_KEYBOARD) - (self.view.frame.size.height - kbHeight);
    
    // 取得键盘的动画时间，这样可以在视图上移的时候更连贯
    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //将视图上移计算好的偏移
//    if(offset > 0) {
        [UIView animateWithDuration:duration animations:^{
            self.view.frame = CGRectMake(0.0f, -250, self.view.frame.size.width, self.view.frame.size.height);
        }];
//    }
}

///键盘消失事件
- (void) keyboardWillHide:(NSNotification *)notify {
    // 键盘动画时间
    double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //视图下沉恢复原状
    [UIView animateWithDuration:duration animations:^{
        self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [MessageField resignFirstResponder];
}

@end